<?php

/**
 * 
 *
 * @version $Id$
 * @copyright 2007
 */

class Authenticate extends Cosmo {
	private $_session ;
	private $_visiteur ;
	private $db ;
	private $config = array();
	public function __construct() {
		parent::__construct() ;
		$this->_session = $this->load->object('Session') ;
		$this->db = $this->load->database() ;
		$this->config = $this->load->config('authenticate') ;
	}
	public function connect($username,$password) {
		switch(strtolower($this->config['auth_encryption_mode'])) {
			case 'md5' :
				$encrypted_pass = md5($password) ;
				break ;
			default : $encrypted_pass = $password ;
		}
		$query = $this->db->query("SELECT ".$this->config['auth_primarykey']." FROM ".$this->config['auth_table']." WHERE ".$this->config['auth_username']."='".$username."' AND ".$this->config['auth_password']."='".$encrypted_pass."'") ;
		if($query->getNumberOfRows() == 1) {
			$result = $query->getResultByObject() ;
			$this->_session->write('id',$result->{$this->config['auth_primarykey']}) ;
			return true;
		} else {
			return false;
		}
	}
	public function logoff() {
		$this->_session->destroy() ;
	}
}
?>